package com.system.log.entity.DTO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.system.log.entity.Log;
public class LogDTO {
	private Long logId;
    private String content;//日志内容  
    private int operationType;
    private String remarks; 
    @DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss")
    private Date startTime;//日期  
    @DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss")
    private Date endTime;//日期  

	public Long getLogId() {
		return logId;
	}

	public String getContent() {
		return content;
	}

	public int getOperationType() {
		return operationType;
	}

	public String getRemarks() {
		return remarks;
	}
	@JsonFormat(pattern="yyyy/MM/dd HH:mm:ss",timezone="GMT+8")
	public Date getStartTime() {
		return startTime;
	}
	@JsonFormat(pattern="yyyy/MM/dd HH:mm:ss",timezone="GMT+8")
	public Date getEndTime() {
		return endTime;
	}

	public void setLogId(Long logId) {
		this.logId = logId;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setOperationType(int operationType) {
		this.operationType = operationType;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public static Specification<Log> getSpecification(LogDTO logDTO){
		Specification<Log> spec = new Specification<Log>() {
			@Override
			public Predicate toPredicate(Root<Log> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				List<Predicate> list = new ArrayList<Predicate>();
				if(null!=logDTO) {
					if(!StringUtils.isEmpty(logDTO.getContent())) {
						Predicate p1 = cb.like(root.get("content").as(String.class), "%"+logDTO.getContent()+"%");
						list.add(p1);
					}
					if(!StringUtils.isEmpty(logDTO.getRemarks())) {
						Predicate p1 = cb.like(root.get("remarks").as(String.class), "%"+logDTO.getRemarks()+"%");
						list.add(p1);
					}
					if(logDTO.getStartTime()!=null) {
						Predicate p1 = cb.greaterThanOrEqualTo(root.get("createdate").as(Date.class),logDTO.getStartTime());
						list.add(p1);
					}
					if(logDTO.getEndTime()!=null) {
						Predicate p1 = cb.lessThanOrEqualTo(root.get("createdate").as(Date.class),logDTO.getEndTime());
						list.add(p1);
					}
				}
				
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		};
		return spec;
	}
}

